package com.lishui.waterojbackendserviceclient.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.lishui.waterojbackendcommon.contant.MessageConstant;
import com.lishui.waterojbackendcommon.exception.BusinessException;
import com.lishui.waterojbackendmodel.model.enumeration.UserRoleEnum;
import com.lishui.waterojbackendmodel.model.pojo.dto.UserLoginDTO;
import com.lishui.waterojbackendmodel.model.pojo.dto.UserRegisterDTO;
import com.lishui.waterojbackendmodel.model.pojo.dto.UserUpdateDTO;
import com.lishui.waterojbackendmodel.model.pojo.entity.User;
import com.lishui.waterojbackendmodel.model.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.lishui.waterojbackendcommon.contant.UserConstant.USER_LOGIN_STATE;

/**
 * 用户服务
 */
@FeignClient(name = "wateroj-backend-user-service", path = "/api/user/inner")
public interface UserFeignClient {

    /**
     * 根据用户获取Id
     * @param userId
     * @return
     */
    @GetMapping("/get/id")
    User getById(@RequestParam("userId") long userId);

    /**
     * 获取当前登录用户
     *
     * @param request
     * @return
     */
    default UserVO getCurrentUser(HttpServletRequest request) {
        // 先判断是否已登录
        Object userObj = request.getSession().getAttribute(USER_LOGIN_STATE);
        User currentUser = (User) userObj;
        if (currentUser == null || currentUser.getId() == null) {
            throw new BusinessException(MessageConstant.USER_NOT_LOGIN);
        }
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(currentUser, userVO);
        // 可以考虑在这里做全局权限校验
        return userVO;
    }

    /**
     * 是否为管理员
     *
     * @param user
     * @return
     */
    default boolean isAdmin(Long userId) {
        User user = this.getById(userId);
        return user != null && UserRoleEnum.ADMIN.getValue().equals(user.getUserRole());
    }
}
